#include <unordered_set>
using namespace std;
bool isHappy(int n) {
    int sum = 0;
    unordered_set<int> s;
    while (sum != 1) {
        sum = 0;
        while (n) {
            int tmp = n % 10;
            n /= 10;
            sum = (sum + tmp * tmp);
        }
        n = sum;
        if (s.find(n) != s.end()) {
            return false;
        }
        s.insert(n);
    }
    return true;
}
